﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class Act_Shutdown_Edit : Form
    {
        public string SelectedSheetNo = "";
        public bool isAdd = true;
        public string reportId = string.Empty;
        HTF_DAL HtfData = new HTF_DAL();
        private Act_Shutdown act_shutdowm;

        public Act_Shutdown_Edit(Act_Shutdown act_form,string id)
        {
            InitializeComponent();
            act_shutdowm = act_form;
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;
                BindReport(reportId);
              
            }
        }

        private void BindReport(string id)
        {   
            var shutdown = DBClass.DBHelper.GetShutdown(id).FirstOrDefault();
            if (shutdown!=null)
            {
                date_start.Text = shutdown.DownStartTime.ToString();
                date_end.Text = shutdown.DownEndTime.ToString();
                txt_reason.Text = shutdown.DownTimeReason;
                txt_id.Text = shutdown.ID.ToString();
            }
        }       

        private void Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) || e.KeyChar == (char)8 || e.KeyChar == '\b')
            {
                e.Handled = true;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string strLineID = System.Configuration.ConfigurationManager.AppSettings["LineID"].ToString();          
            string strStartTime = date_start.Text.Trim();
            string strEndTime = date_end.Text.Trim();
            string strReason = txt_reason.Text.Trim();          

            if (string.IsNullOrEmpty(strStartTime) )
            {
                MessageBox.Show("请输入停机开始时间！");
                return;
            }
            if (string.IsNullOrEmpty(strEndTime))
            {
                MessageBox.Show("请输入停机结束时间！");
                return;
            }
            if (string.IsNullOrEmpty(strReason))
            {
                MessageBox.Show("请输入停机原因！");
                return;
            }

            int duration = Int32.Parse( (DateTime.Parse(strEndTime) - DateTime.Parse(strStartTime)).TotalMinutes.ToString());

            if (duration <= 0)
            {
                MessageBox.Show("结束时间必须在开始时间之后！");
                return;
            }
          
            try
            {
                string sql = string.Empty;              
                DBEntity.Models.ACT_SHUTDOWN shutdown = new DBEntity.Models.ACT_SHUTDOWN();
                if (!isAdd)
                {   
                    shutdown.ID =Int64.Parse( txt_id.Text);
                    shutdown.LineID = strLineID;
                    shutdown.DownStartTime = DateTime.Parse( strStartTime);
                    shutdown.DownEndTime = DateTime.Parse(strEndTime);
                    shutdown.DownTimeDuration = duration;
                    shutdown.DownTimeReason = strReason;
                    bool result = DBClass.DBHelper.UpdateShutdown(shutdown);
                    if (result )
                    {                        
                        //DBClass.DBHelper.SendBufferToL3("ACT_SHUTDOWN", shutdown.ID, "U");
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        act_shutdowm.btn_query_Click(null, null);
                    }
                }
                else
                {
                    shutdown.LineID = strLineID;
                    shutdown.DownStartTime = DateTime.Parse(strStartTime);
                    shutdown.DownEndTime = DateTime.Parse(strEndTime);
                    shutdown.DownTimeDuration = duration;
                    shutdown.DownTimeReason = strReason;
                    long id = DBClass.DBHelper.InsertShutdown(shutdown);
                    if (id > 0)
                    {                       
                        //DBClass.DBHelper.SendBufferToL3("ACT_SHUTDOWN", id, "U");
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        act_shutdowm.btn_query_Click(null, null);
                    }                            
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存停机信息失败！"+ex.Message);  
            }
        }
    }
}
